
/*
 * GeneratePanel.java
 *
 * Created on 26 juin 2010, 13:55:23
 */
package darknet.gui.wizard.firstrun;

import darknet.DarknetRegistry;
import darknet.core.impl.FacadeImpl;
import darknet.core.spec.Facade;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JOptionPane;

/**
 * 
 * @author Yoyo
 */
public class GeneratePanel extends javax.swing.JPanel {

    /** Reference to the wizard Dialog. */
    private WizardFirstRunDialog wizard;

    /** Creates new form GeneratePanel */
    public GeneratePanel(WizardFirstRunDialog wizard) {
        this.wizard = wizard;
        initComponents();
    }

    /**
     * Writer parameters, start files generation and get a facade instance.
     */
    public void start() {

        btnNext.setEnabled(false);

        DarknetRegistry registry = null;
        Facade facade = null;

        GeneratePanel.this.progressbar.setValue(0);
        GeneratePanel.this.progressbar.setString("0%");

        // First task
        wizard.writeParameters();
        GeneratePanel.this.labelFeedback.setText("Writing parameters ...");
        GeneratePanel.this.progressbar.setValue(33);
        GeneratePanel.this.progressbar.setString("33%");
        GeneratePanel.this.labelFeedback.setText("Writing parameters OK");

        // Create the Facade and the Registry.
        String workingDirectory = System.getProperty("user.dir")
                + File.separator + "conf";
        registry = new DarknetRegistry(workingDirectory);
        try {

            // Second task
            GeneratePanel.this.labelFeedback.setText("Facade creation ...");
            facade = new FacadeImpl(
                    registry.getWorkingDirectory().getAbsolutePath(),
                    registry.getUsername(),
                    registry.getPassword(),
                    registry.getAddress(),
                    registry.getPort());

            GeneratePanel.this.progressbar.setValue(66);
            GeneratePanel.this.progressbar.setString("66%");
            GeneratePanel.this.labelFeedback.setText("Facade creation ok");

        } catch (FileNotFoundException e) {

            JOptionPane.showMessageDialog(
                    null, "An error occur while starting core services : \n"
                    + e.getLocalizedMessage(), "Darknet 0.1 - Fatal error",
                    JOptionPane.ERROR_MESSAGE);
            System.exit(-1);

        } catch (IOException e) {

            JOptionPane.showMessageDialog(
                    null, "An error occur while starting core services : \n"
                    + e.getLocalizedMessage(), "Darknet 0.1 - Fatal error",
                    JOptionPane.ERROR_MESSAGE);
            System.exit(-1);
        }

        // Third task.
        GeneratePanel.this.labelFeedback.setText("Setting facade...");
        wizard.getMainFrame().setFacade(facade);
        GeneratePanel.this.progressbar.setValue(99);
        GeneratePanel.this.progressbar.setString("99%");
        GeneratePanel.this.labelFeedback.setText("Setting facade OK");

        GeneratePanel.this.progressbar.setValue(100);
        GeneratePanel.this.progressbar.setString("100%");
        GeneratePanel.this.labelFeedback.setText("All done");

        GeneratePanel.this.btnNext.setEnabled(true);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        wizardIcon = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();
        btnNext = new javax.swing.JButton();
        btnPrevious = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        progressbar = new javax.swing.JProgressBar();
        labelFeedback = new javax.swing.JLabel();

        wizardIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/darknet/gui/wizard/firstrun/wizard.png"))); // NOI18N

        jEditorPane1.setContentType("text/html");
        jEditorPane1.setEditable(false);
        jEditorPane1.setText("<html>\r\n  <head>\r\n  </head>\r\n  <body>\r\n  <h3>Generating Files (3/3)</h3>\n  <p>\nThe files are generating.<br />\n<br />\nPlease wait...\t\n  </p>\n  </body>\r\n</html>\r\n");
        jScrollPane2.setViewportView(jEditorPane1);

        btnNext.setText("Next");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        btnPrevious.setText("Previous");
        btnPrevious.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPreviousActionPerformed(evt);
            }
        });

        jLabel1.setText("Progression :");

        progressbar.setStringPainted(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(wizardIcon)
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnPrevious, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1)
                    .addComponent(progressbar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(labelFeedback, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(28, 28, 28))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(wizardIcon)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(progressbar, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelFeedback)))
                .addContainerGap(33, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(272, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPrevious, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        wizard.showFinish();
}//GEN-LAST:event_btnNextActionPerformed

    private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPreviousActionPerformed
        wizard.showNetwork();
}//GEN-LAST:event_btnPreviousActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrevious;
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel labelFeedback;
    private javax.swing.JProgressBar progressbar;
    private javax.swing.JLabel wizardIcon;
    // End of variables declaration//GEN-END:variables
}
